(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;	
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
			initCourse();
		});
	});
	function initCourse(){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var classcode = $(".students-choice-select",parent.document).attr("classcode");
		var data = {
				cmd:"getCourseByStudentCode",
				model:"course",
				userKey:userKey,
				data:{
					classcode:classcode,
					schoolcode:schoolcode
				},
				remark:""
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "<tr style='font-weight: 700;'>" +
								"<td width='50px'>序号</td>" +
								"<td width='100px'>时间安排</td>" +
								"<td width='100px'>星期一</td>" +
								"<td width='100px'>星期二</td>" +
								"<td width='100px'>星期三</td>" +
								"<td width='100px'>星期四</td>" +
								"<td width='100px'>星期五</td>" +
							"</tr>";
			for(var j = 0; j < 8; j++){
				content += "<tr>" + 
						"<td width='50px'>" + (j+1) + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-date' courseno='" + (j+1) + "' width='121px'>" + getDefaultTime(j+1) + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-point' coursecode='' subjectcodeno='' courseno='" + (j+1) + "' weekno='1' width='100px'>" + "" + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-point' coursecode='' subjectcodeno='' courseno='" + (j+1) + "' weekno='2' width='100px'>" + "" + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-point' coursecode='' subjectcodeno='' courseno='" + (j+1) + "' weekno='3' width='100px'>" + "" + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-point' coursecode='' subjectcodeno='' courseno='" + (j+1) + "' weekno='4' width='100px'>" + "" + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-point' coursecode='' subjectcodeno='' courseno='" + (j+1) + "' weekno='5' width='100px'>" + "" + "</td>" +
					"</tr>";
			}
			$(".course-table-body").empty();
			$(".course-table-body").append(content);
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var data = res.data;
				var list = data.course;
				var className = res.data.calssName;
				$("#course-table-title").text(className + "的课程表");
				for(var i = 0; i < list.length; i++){
					for(var key in list[i]){
						if(list[i][key] == null || list[i][key] == "null"){
							list[i][key] = "";
						}
					}
					var courseIndex = parseInt(list[i]["courseno"]*7) + parseInt(list[i]["weekno"]) + 1;
					var timeIndex = parseInt(list[i]["courseno"]*7) + 1;
					var subjectcodeno = list[i]["subjectcodeno"];
					var coursecode = list[i]["coursecode"];
					$(".course-table-body").find("td")[courseIndex].innerHTML = list[i]["subjectcode"];
					$(".course-table-body").find("td")[courseIndex].setAttribute("subjectcodeno",subjectcodeno);
					$(".course-table-body").find("td")[courseIndex].setAttribute("coursecode",coursecode);
					$(".course-table-body").find("td")[timeIndex].innerHTML = list[i]["coursetime"];
				}
			}			
			
		},null,null);
	}
	function getDefaultTime(a){
		switch (a){
			case 1:
				return "08:00-08:45";
				break;
			case 2:
				return "08:55-09:40";
				break;
			case 3:
				return "10:00-10:45";
				break;
			case 4:
				return "10:55-11:40";
				break;
			case 5:
				return "13:30-14:15";
				break;
			case 6:
				return "14:25-15:10";
				break;
			case 7:
				return "15:30-16:15";
				break;
			case 8:
				return "16:25-17:10";
				break;
		}
	}
	
	function pagebtns(){		
		$(".students-choice",parent.document).unbind("click");
		$(".students-choice",parent.document).click(function(){
			$(".students-choice",parent.document).removeClass("classes-choice-select");
			$(this).addClass("students-choice-select");
			initCourse();
			
		});	
	}
	
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
})();